<template>
  <div class="param-main">
    <!-- <el-breadcrumb separator-class="el-icon-arrow-right" style="padding: 0 0 9px 9px;">
      <transition-group name="breadcrumb">
        <el-breadcrumb-item v-for="(item, index) in levelList" :key="index">
          <span @click="goBack(item.path)">{{ item.name }}</span>
        </el-breadcrumb-item>
      </transition-group>
    </el-breadcrumb> -->
    <el-form
      ref="tblMain"
      :model="serachForm"
      labelPosition="right"
      inline=""
      label-suffix=":"
      label-width="110px"
    >
      <el-row :span="24">
        <el-col :span="8">
          <el-form-item label="品牌名称">
            <el-select
              v-model="serachForm.letterId"
              size="small"
              placeholder="请选择"
              filterable
              @change="getCarjBrandList"
            >
              <el-option
                v-for="item in letterList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="车系名称">
            <el-select
              v-model="serachForm.brandId"
              size="small"
              placeholder="请选择"
              filterable
            >
              <el-option
                v-for="item in brands"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="车型名称">
            <el-input
              v-model="serachForm.vehicleModel"
              size="small"
              placeholder=""
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <div class="page-pane2">
        <el-button type="primary" size="small" @click="initTableData()"
          >查询</el-button
        >
        <el-button size="small" @click="dataReset()">清空</el-button>
        <el-button size="small" type="info" @click="detailTblMain(1)">新增</el-button>
        <el-button size="small" type="danger" @click="deleteEvent(1)"
          >删除</el-button
        >
      </div>
    </el-form>
    <div style="height: calc(100% - 160px)">
      <el-table
        ref="tblMain"
        style="margin-top: 10px"
        :data="listData"
        height="100%"
        @selection-change="handleSelectionChange"
        @sort-change="getSortList"
        v-loading.body="loading"
        border
        stripe
      >
        <el-table-column
          type="selection"
          width="50"
          align="center"
        ></el-table-column>
        <el-table-column
          type="index"
          width="50"
          label="序号"
          align="center"
        ></el-table-column>
        <el-table-column label="操作" align="center" width="200px" >
          <template slot-scope="scope">
            <el-button  type="text" @click="detailTblMain(2,scope.row)" >编辑</el-button>
            <el-divider direction="vertical" ></el-divider>
            <el-button  type="text" @click="deleteEvent(scope.row)">删除</el-button>
            <el-divider direction="vertical"></el-divider>
            <el-button  type="text" @click="detailTblMain(3,scope.row)">明细</el-button>
          </template>
        </el-table-column>
        <el-table-column
          prop="vehicleModel"
          label="车型名称"
          align="center"
          width="120"
          show-overflow-tooltip
          sortable="custom"
        >
          <template slot-scope="scope">
            {{ scope.row.vehicleModel }}
          </template>
        </el-table-column>
        <el-table-column
          prop="year"
          label="年份"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="engine"
          label="发动机"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="displacement"
          label="排量"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="frontBrake"
          label="前制动器"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="rearBrake"
          label="后制动器"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="bodyForm"
          label="车身形式"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="driveMethod"
          label="驱动方式"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="parkingBrake"
          label="驻车制动"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="power"
          label="功率"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="gatherTime"
          label="采集时间"
          align="center"
          width="160"
        ></el-table-column>
        <el-table-column
          prop="gatherFlag"
          label="是否采集"
          align="center"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            {{ scope.row.gatherFlag == '1' ? '是' : '否' }}
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- v-show="pageInfo.total != 0" -->
    <el-pagination
      style="margin-top: 10px"
      :current-page.sync="pageInfo.page"
      :page-size="pageInfo.size"
      :total="pageInfo.total"
      layout="total, sizes, prev, pager, next"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>
    <add-vehicle-model
      ref="addVehicleLine"
      v-if="visible"
      @ok="saveData"
      @cancle="cancle"
      :title="title"
      :row="rowData"
    ></add-vehicle-model>
  </div>
</template>
<script>
import AddVehicleModel from './addVehicleModel'
import {
  addCarj,
  delCarj,
  upCarj,
  getCarjList,
  getCarjLetterList,
  getCarjBrandList
} from '@/api/carLine'
import { getDataValue } from '@/api/index'

export default {
  name: 'applyMain',
  props: ['pageData'],
  components: {
    AddVehicleModel
  },
  data() {
    return {
      visible: false,
      rowData: {},
      title: '',
      levelList: [],
      loading: false,
      listData: [],
      selection: [],
      serachForm: {
        vehicleModel: '',
        letterId: '',
        brandId: '',
        order:""
      },
      pageInfo: {
        page: 1,
        size: 10,
        total: 0
      },
      brands: [],
      letterList: []
    }
  },
  watch: {
    pageData() {
      this.initTableData()
    }
  },
  created() {
    this.letterBrandOb = this.$route.query.data
      ? this.$route.query.data
      : {
          letterOb: {
            name: null,
            path: null
          },
          brandOb: {
            name: null,
            path: null
          }
        } //品牌对象
    let letterOp = {
      letterId: this.letterBrandOb.brandOb.letterId
        ? this.letterBrandOb.brandOb.letterId
        : '',
      name: this.letterBrandOb.brandOb.letterName
        ? this.letterBrandOb.brandOb.letterName
        : '所有品牌',
      path: '/product/brand'
    }
    this.levelList.push(letterOp)
    let brandOp = {
      name: this.letterBrandOb.brandOb.name
        ? this.letterBrandOb.brandOb.name
        : '所有车系',
      path: '/product/carSeries'
    }
    this.levelList.push(brandOp)
  },
  mounted() {
    this.getBrandsList().then(() => {
      this.serachForm.letterId = this.letterBrandOb.brandOb.letterId
      this.getCarjBrandList(this.serachForm.letterId)
      this.serachForm.brandId = this.letterBrandOb.brandOb.id
      this.initTableData()
    })
  },
  methods: {
    /**
    *  排序查询
    */
    getSortList(column) {
      let arr = [];
      arr.push(column.prop);
      arr.push( column.order.substring(
           0,
           column.order.indexOf("ending")
       ))
      this.serachForm.order = arr.join(',');
      this.initTableData()
    },
     goBack(path){
      this.$router.push(path)
    },
    /**
     * 点击车型名称跳转到配件页面
     */
    goMr(row) {
      let op = row
      op.path = '/product/vehicleModel'
      let data = {
        letterOb: this.letterBrandOb.letterOb,
        brandOb: this.letterBrandOb.brandOb,
        vehicleOb: op
      }
      this.$router.push({
        path: '/product/partsManage',
        query: {
          data: data
        }
      })
    },
    /**
     * 获取品牌列表信息
     */
    getBrandsList() {
      return new Promise((resolve) => {
        let data = {
          page: 1,
          size: 1000
        }
        getCarjLetterList(data).then((res) => {
          const { code = undefined, data = {} } = res.data
          if (!code) {
            this.letterList = data.content
            resolve()
          } else {
            this.$message.error(data)
          }
        })
      })
    },
    /**
     * 获取车系信息
     */
    getCarjBrandList(letterId) {
      let param = {
        page: 1,
        size: 1000,
        letterId
      }
      getCarjBrandList(param)
        .then((res) => {
          const { code = undefined, data = {} } = res.data
          if (!code) {
            this.brands = data.content
          }
        })
        .catch(() => {
          this.$message.error('服务器繁忙')
        })
    },
    /**
     * 点击功能按钮
     */
    detailTblMain(type,row) {
      if (type != 1) {
        this.title = type == 2 ? '修改车型' : '车型详情'
        this.rowData = JSON.parse(JSON.stringify(row))
      } else {
        this.title = '新建车型'
        this.rowData = {}
      }
      this.visible = true
    },
    /**
     * 保存
     */
    saveData(val) {
      if (this.title == '新建车型') {
        addCarj(val).then((res) => {
          const { code = undefined, data = {} } = res.data
          if (!code) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            this.initTableData()
            this.cancle()
          } else {
            this.$message({
              type: 'error',
              message: '操作失败!'
            })
          }
        })
      } else {
        upCarj(val).then((res) => {
          const { code = undefined, data = {} } = res.data
          if (!code) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            this.initTableData()
            this.cancle()
          } else {
            this.$message({
              type: 'error',
              message: '操作失败!'
            })
          }
        })
      }
    },
    /**
     * 关闭弹框
     */
    cancle() {
      this.visible = false
    },
    /**
     * 多选
     */
    handleSelectionChange(val) {
      this.selection = val
    },
    /**
     * 重置
     */
    dataReset() {
      this.pageInfo.page = 1
      this.pageInfo.size = 10
      this.serachForm.letterId = ''
      this.serachForm.brandId = ''
      this.serachForm.vehicleModel = ''
      this.initTableData()
    },
    /**
     * 分页
     */
    handleSizeChange(val) {
      this.pageInfo.size = val
      this.initTableData()
    },
    /**
     * 分页
     */
    handleCurrentChange(val) {
      this.pageInfo.page = val
      this.initTableData()
    },
    /**
     * 获取表格数据
     */
    initTableData() {
      let me = this
      me.selection = []
      let param = {
        page: me.pageInfo.page,
        size: me.pageInfo.size,
        ...me.serachForm
      }
      me.loading = true
      getCarjList(param)
        .then((res) => {
          me.loading = false
          const { code = undefined, data = {} } = res.data
          if (!code) {
            me.listData = data.content
            me.pageInfo.total = data.totalElements
          }else{
            me.$message.error(data)
          }
        })
        .catch(() => {
          me.$message.error('服务器繁忙')
          me.loading = false
        })
    },
    /**
     * 删除
     */
     deleteEvent(row) {
      let ids = []
      if(row == '1'){
         if (this.selection.length < 1) {
          this.$message.warning('请至少选择一条数据')
          return
        }
        ids = this.selection.map(item=>{
          return item.id
        })
      }else{
        ids.push(row.id)
      }
      this.$confirm('此操作将永久删除该车系, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          delCarj(ids).then((res) => {
            const { code = undefined, data = {} } = res.data
            if (!code) {
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
              this.initTableData()
            } else {
              this.$message({
                type: 'error',
                message: '操作失败!'
              })
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>
